/**
 * @Author: weipc 755197142@qq.com
 * @Date: 2025-04-01 14:52:12
 * @LastEditors: weipc 755197142@qq.com
 * @LastEditTime: 2025-04-01 14:58:26
 * @FilePath: src/common/typeorm/typeorm.config.ts
 * @Description: 这是默认设置,可以在设置》工具》File Description中进行配置
 */
import 'winston-daily-rotate-file';
import { WINSTON_MODULE_NEST_PROVIDER, WinstonLogger } from 'nest-winston';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleAsyncOptions } from '@nestjs/typeorm';
import TypeormLogger from '@/common/typeorm/typeorm.logger';

export const typeormConfig: TypeOrmModuleAsyncOptions = {
  useFactory: (configService: ConfigService, logger: WinstonLogger) => {
    return {
      type: 'mysql',
      timezone: '+08:00',
      host: configService.get('db.mysql.host'),
      port: configService.get('db.mysql.port'),
      username: configService.get('db.mysql.user'),
      password: configService.get('db.mysql.password'),
      database: configService.get('db.mysql.database'),
      synchronize: configService.get('db.mysql.synchronize'),
      logging: configService.get('db.mysql.logging', true),
      logger: new TypeormLogger(logger),
      entities: [`${__dirname}/**/*.entity{.ts,.js}`],
      autoLoadEntities: true,
      keepConnectionAlive: false,
      poolSize: configService.get('db.mysql.pool_size', 10),
      connectorPackage: 'mysql2',
      extra: {
        // authPlugin: 'sha256_password',
        connectionLimit: configService.get('db.mysql.pool_size', 10),
        queueLimit: 100, // 限制等待队列大小
      },
    };
  },
  inject: [ConfigService, WINSTON_MODULE_NEST_PROVIDER],
};
